package com.example.com.skills_utf8;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.app.net.NetHelper;
import com.app.view.ImageViewRound;
import com.utils.UtilsTipView;

 

public class _49Activity extends Activity {

	private _49Activity activity = this;
	
	
	private LinearLayout layout_title;
	private Handler handler;
	private FrameLayout layout_content;


	private Button btn_generate;

	
	private ArrayList<Root> listdata = new ArrayList<Root>();


	private Adapter adapter;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_49);
		
		
		/*	需要依赖工程
		if (!LibsChecker.checkVitamioLibs(this))
			return;
		*/
		
		layout_title = (LinearLayout)findViewById(R.id.layout_title);
		layout_content = (FrameLayout)findViewById(R.id.layout_content);
		btn_generate = (Button)findViewById(R.id.btn_generate);
		
		
		handler = new Handler();     
		
		btn_generate.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			
				UtilsTipView.getInstance().play(activity, handler, layout_content);
			
			}
		});
		
		ListView listview = (ListView)findViewById(R.id.listview);
		listview.setOnScrollListener(new OnScrollListener() {
			
			int firstVisibleItem = -1;
			int Last_LastVisiablePosition = -1;
			boolean isRunning = false;
			
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				
				
				if(scrollState == SCROLL_STATE_IDLE)
					isRunning = false;
				else{
					isRunning = true; 
				}

				//System.out.println("isRunning: "+isRunning);
				
				
				//System.out.println("onScrollStateChanged");
				if( isRunning == false  ){ 
					adapter.Update( Last_LastVisiablePosition );
				}
				
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				//System.out.println("onScroll");
			
				//view.findViewById(R.id.video_view);
				this.firstVisibleItem = firstVisibleItem;
				
				int LastVisiablePosition = view.getLastVisiblePosition();
			//	System.out.println("firstVisibleItem: "+firstVisibleItem);
			//	System.out.println("LastVisiablePosition: "+LastVisiablePosition);
			//	System.out.println("totalItemCount: "+totalItemCount);
				
				///*
				if( LastVisiablePosition > Last_LastVisiablePosition){
					System.out.println("向上滑动 ");
					//Utils1.showToastShort(activity, "向上滑动");
				}
				
				if(LastVisiablePosition < Last_LastVisiablePosition){
					System.out.println("向下滑动");
					//Utils1.showToastShort(activity, "向下滑动");
				}
				
				Last_LastVisiablePosition = LastVisiablePosition;
				
				
				/*
				if(isRunning == false){
					View v_child = view.getChildAt(firstVisibleItem); 
					if(v_child != null){
					VideoView videoview = (VideoView)v_child.findViewById(R.id.video_view);
					if(videoview != null){
						System.out.println("video play.");
						videoview.start();
					}
					}
				}
				*/
				//*/
				
				
			}
		});
		
		
		
		/*
		for(int i =0; i<10; i++){
			Root Root = new Root();
			Root.pic = "file:///android_asset/0019.jpg"; 
			//Root.url_video = "android.resource://"+getPackageName()+"/"+R.raw.sasasa;
			//Root.url_video = getExternalCacheDir().toString() + "/legacy/tencent/MicroMsg/sasasa.mp4";
			Root.url_video = "http://10.127.127.1/api/sasasa.mp4";
			listdata.add(Root);
		}
		*/


		adapter = new Adapter();
		listview.setAdapter(adapter);
		
		
		
		/*
		VideoView videoview1 = (VideoView)findViewById(R.id.videoview1);
		videoview1.setVideoPath("http://10.127.127.1/api/sasasa.mp4");
		*/


		requestNetDate();
		
		
	}
	
	

	void requestNetDate(){

		new NetHelper(this){

			@Override
			public String init(ArrayList<BasicNameValuePair> arg0) {
				// TODO Auto-generated method stub

				return "http://waqu.com/v2.1/m/and/v/general_and/pv.json?ts=1424190570556&partner=0009&appName=guitar&appV=3.3.7&hash=087dd31b744e7298168f1fea705a5c92439a5598&sid=14f65a8f495d&size=5&pos=0&scanned=w4hrm8uq3skw09u8~z8655v76h271qp9r~pmox1t85u70ofejs~1j1fqughjkf4ropc~i93yyduvm77f3lps~";
			}

			@Override
			public void onComplete(String arg0) throws JSONException {
				// TODO Auto-generated method stub


				/*
				Gson gson = new Gson();
				Root Root = (Root)gson.fromJson(arg0, Root.class);

				System.out.println(Root.toString());
				*/



			}

		}.start_GET();

	}



	
	

	class Adapter extends BaseAdapter
	{

		int position_video = 0 ;
		public void Update(int pos){
			position_video = pos;
			notifyDataSetChanged();
		}


		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listdata.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listdata.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.adapter_49, null);

			}

			Root Root = listdata.get(position);

			/*
			VideoView mVideoView = (VideoView)convertView.findViewById(R.id.video_view);
			if(position == position_video){
				mVideoView.setVideoPath( Root.url_video );
				mVideoView.start();
				System.out.println("video play: " +position);
			}
			*/

			ImageView img_video = (ImageView)convertView.findViewById(R.id.img_video);



			ImageViewRound ImageView = (ImageViewRound)convertView.findViewById(R.id.iv_user_pic);
		//	ImageView.SetAttr(5, -1, Color.BLUE);
			ImageView.setImageResource(R.drawable.userpic);


			return convertView;
		}

	};



	class Root
	{

	};
	
 
	
	
	
}




